<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
class AdminController extends Controller {

    public function index()
    {
        $this->display('index');
    }

    public function welcome()
    {
        $page_title = '我的桌面';
        $this->display('welcome');
    }
    public function login()
    {
        $username = trim($_POST['username']);
        $admin = M('admin');



        $check = $this->check_verify($_POST['verify_code']);
        if(!$check){
            $this->error('验证码错误！！');
        }

        $map=array("username" => $username);
        $admin_info = $admin
            ->where($map)
            ->find();


        if($admin_info['password'] == md5(trim($_POST['password']))){  //密码正确
          $_SESSION['admin'] = TRUE;
          $_SESSION['username'] = $username;
          $_SESSION['school_id'] = $admin_info['school_id'];
          $_SESSION['role'] = $admin_info['role'];

          $log = M('log');
            $user['user_id'] = $admin_info['id'];
            $user['school_id'] = $admin_info['school_id'];
            $user['username'] = $admin_info['username'];
            $user['IP']= ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
            $user['datetime'] = date("Y-m-d H:i:s");
            $user['memo'] = $this->get_os() ."系统，浏览器：". $this->get_broswer();
            $log->add($user);
            //写入登陆记录
            /*
                      if($_SESSION['role']==1 || $_SESSION['role']==0){
                          $this->success('登陆成功',U('Admin/Kucun/showAdd'));
                      }
                      if($_SESSION['role']==2){
                          $this->success('登陆成功',U('Admin/Stu/showAll'));
                      }
                      if($_SESSION['role']==3){
                          $this->success('登陆成功',U('Admin/Kucun/showAll'));
                      }
            */
            $this->success('登陆成功',U('Admin/admin/index'));

       //  $this-> display('index');//display下面跟模板
        }else{
          $_SESSION['admin'] = FALSE;
          $this->error('用户名或者密码不对！！');
        }

    }

    public function logout()
    {
            $_SESSION['admin'] = FALSE;
            $this->success('退出成功',U('Admin/Index'));
           // $this->redirect('index');//重定向到控制器的方法  display是本控制的方法

    }
//自助密码修改
    public function showPwdUpdate()
    {
        $this->check_auth(); //检查权限
        $stu=M('admin');
        $user_list=$stu->where("username='".$_SESSION['username']."'")->find();
        $this->assign('user_list',$user_list);
        $this-> assign('page_title',"修改");

        $this->display('showPwdUpdate');
    }

    public function adminlist()
    {
        $this->check_auth(); //检查权限
        $role_array=array(
            "0" => "超级管理员",
            "1" => "学校管理员",
            "2" => "学籍管理员",
            "3" => "营养餐管理员"
        );

        $user=M('admin');
        import('Org.Util.Page');
        $total=$user->count();//记录总数
        $num_pre_page=15;//每页显示8条记录
        $page=new Page($total,15);
        $page->setConfig("header","用户管理");
        $pageControl=$page->show();
        $map = array();
        $users = $user
            ->alias('t')
            ->field('t.*,c.name as school_name,c.tel as school_tel')
            ->join('left join '.C('DB_PREFIX').'school as c on c.id = t.school_id')
            ->where($map)
            ->order("t.id ASC")
            ->limit("$page->firstRow,$page->listRows")
            ->select();
        //   echo $user->_sql();

        //处理班级主任年级主任问题
        foreach($users as $k=>$userinfo)
        {
            $users[$k]['role'] = $role_array[$userinfo['role']];
        }
        $this-> assign('page_title',"管理用户列表");
        $this->assign('pageControl', $pageControl);
        $this->assign('users',$users);
        $this->display();

    }
    public function school()
    {
        $this->check_auth(); //检查权限
        $user=M('school');
        import('Org.Util.Page');
        $total=$user->count();//记录总数
        $num_pre_page=15;//每页显示8条记录
        $page=new Page($total,15);
        $page->setConfig("header","学校管理");
        $pageControl=$page->show();
        $map = array();
        $users = $user
            ->alias('t')
            ->where($map)
            ->order("t.id DESC")
            ->limit("$page->firstRow,$page->listRows")
            ->select();

        $this-> assign('page_title',"学校管理");
        $this->assign('pageControl', $pageControl);
        $this->assign('users',$users);
        $this->display();

    }

    public function showAdd()
    {
        $this->check_auth(); //检查权限
        $school_class = M("school");
        $school_class_array = $school_class->select();
        $this->assign('school_class_array',$school_class_array);
        $this-> assign('page_title',"添加");
        $this->display('update');

    }

    //实现学生修改功能
    public function update()
    {
        $this->check_auth(); //检查权限
        header("Content-Type:text/html;charset=utf-8");
        $stu=M('admin');
        $id=$_GET['id'];
        if($id && $id>=22){
            //修改
            if($_POST['password']!=""){
                $_POST['password'] = md5($_POST['password']);
            }else{
                unset($_POST['password']);
            }
            $stu_list=$stu->where("id=$id")->save($_POST);
            if($stu_list){
                $this->success('修改成功！！');
                $this->redirect('/Admin/Admin/adminlist');
            }else{
                $this->error('修改失败！！');
            }
        }else{
            $stuinfo['username']=$_POST['username'];
            $stuinfo['password']=md5($_POST['password']);
            $stuinfo['role']=$_POST['role'];
            $stuinfo['school_id']=$_POST['school_id'];
            $result = $stu->add($stuinfo);
            if($result)
            {
                $this->success('添加成功！！');
                $this->redirect('/Admin/Admin/adminlist');
            }else{
                $this->error('添加失败！！');
            }
        }

    }

    //自助密码修改
    public function pwdupdate()
    {
        $this->check_auth(); //检查权限
        header("Content-Type:text/html;charset=utf-8");
        $stu=M('admin');

            //修改
            if($_POST['password']!=""){
                $_POST['password'] = md5($_POST['password']);
            }else{
                unset($_POST['password']);
            }
            $admin_user = array("password" => $_POST['password']);
            $stu_list=$stu->where("username='".$_SESSION['username']."'")->save($admin_user);

            if($stu_list){
                $this->success('修改成功！！');
                $this->redirect('/Admin/Admin/logout');
            }else{
                $this->error('修改失败！！');
            }


    }

    public function showUpdate()
    {
        $this->check_auth(); //检查权限
        $stu=M('admin');
        $id=$_GET['id'];
        $user_list=$stu->where("id=$id")->find();

        $school_class = M("school");
        $school_class_array = $school_class->select();
        $this->assign('school_class_array',$school_class_array);

        $this->assign('user_list',$user_list);
        $this-> assign('page_title',"修改");

        $this->display('update');
    }


    public function del()
    {
        $this->check_auth(); //检查权限
        $stu=M('admin');
        $id=$_GET['id'];
        if($id==22){
            $this->error('无权操作！！');
        }
        $stu->where("id=$id")->delete();
        $this->redirect('User:adminlist');//重定向到控制器的方法  display是本控制的方法
    }

//校验验证码
    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    /**
     * 获取客户端浏览器信息 添加win10 edge浏览器判断
     * @param  null
     * @author  Jea杨
     * @return string
     */
    function get_broswer(){
        $sys = $_SERVER['HTTP_USER_AGENT'];  //获取用户代理字符串
        if (stripos($sys, "Firefox/") > 0) {
            preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
            $exp[0] = "Firefox";
            $exp[1] = $b[1];  //获取火狐浏览器的版本号
        } elseif (stripos($sys, "Maxthon") > 0) {
            preg_match("/Maxthon\/([\d\.]+)/", $sys, $aoyou);
            $exp[0] = "傲游";
            $exp[1] = $aoyou[1];
        } elseif (stripos($sys, "MSIE") > 0) {
            preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
            $exp[0] = "IE";
            $exp[1] = $ie[1];  //获取IE的版本号
        } elseif (stripos($sys, "OPR") > 0) {
            preg_match("/OPR\/([\d\.]+)/", $sys, $opera);
            $exp[0] = "Opera";
            $exp[1] = $opera[1];
        } elseif(stripos($sys, "Edge") > 0) {
            //win10 Edge浏览器 添加了chrome内核标记 在判断Chrome之前匹配
            preg_match("/Edge\/([\d\.]+)/", $sys, $Edge);
            $exp[0] = "Edge";
            $exp[1] = $Edge[1];
        } elseif (stripos($sys, "Chrome") > 0) {
            preg_match("/Chrome\/([\d\.]+)/", $sys, $google);
            $exp[0] = "Chrome";
            $exp[1] = $google[1];  //获取google chrome的版本号
        } elseif(stripos($sys,'rv:')>0 && stripos($sys,'Gecko')>0){
            preg_match("/rv:([\d\.]+)/", $sys, $IE);
            $exp[0] = "IE";
            $exp[1] = $IE[1];
        }else {
            $exp[0] = "未知浏览器";
            $exp[1] = "";
        }
        return $exp[0].'('.$exp[1].')';
    }

    /**
     * 获取客户端操作系统信息包括win10
     * @param  null
     * @author  Jea杨
     * @return string
     */
    function get_os(){
        $agent = $_SERVER['HTTP_USER_AGENT'];
        $os = false;

        if (preg_match('/win/i', $agent) && strpos($agent, '95'))
        {
            $os = 'Windows 95';
        }
        else if (preg_match('/win 9x/i', $agent) && strpos($agent, '4.90'))
        {
            $os = 'Windows ME';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/98/i', $agent))
        {
            $os = 'Windows 98';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent))
        {
            $os = 'Windows Vista';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent))
        {
            $os = 'Windows 7';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent))
        {
            $os = 'Windows 8';
        }else if(preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent))
        {
            $os = 'Windows 10';#添加win10判断
        }else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent))
        {
            $os = 'Windows XP';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent))
        {
            $os = 'Windows 2000';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent))
        {
            $os = 'Windows NT';
        }
        else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent))
        {
            $os = 'Windows 32';
        }
        else if (preg_match('/linux/i', $agent))
        {
            $os = 'Linux';
        }
        else if (preg_match('/unix/i', $agent))
        {
            $os = 'Unix';
        }
        else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent))
        {
            $os = 'SunOS';
        }
        else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent))
        {
            $os = 'IBM OS/2';
        }
        else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent))
        {
            $os = 'Macintosh';
        }
        else if (preg_match('/PowerPC/i', $agent))
        {
            $os = 'PowerPC';
        }
        else if (preg_match('/AIX/i', $agent))
        {
            $os = 'AIX';
        }
        else if (preg_match('/HPUX/i', $agent))
        {
            $os = 'HPUX';
        }
        else if (preg_match('/NetBSD/i', $agent))
        {
            $os = 'NetBSD';
        }
        else if (preg_match('/BSD/i', $agent))
        {
            $os = 'BSD';
        }
        else if (preg_match('/OSF1/i', $agent))
        {
            $os = 'OSF1';
        }
        else if (preg_match('/IRIX/i', $agent))
        {
            $os = 'IRIX';
        }
        else if (preg_match('/FreeBSD/i', $agent))
        {
            $os = 'FreeBSD';
        }
        else if (preg_match('/teleport/i', $agent))
        {
            $os = 'teleport';
        }
        else if (preg_match('/flashget/i', $agent))
        {
            $os = 'flashget';
        }
        else if (preg_match('/webzip/i', $agent))
        {
            $os = 'webzip';
        }
        else if (preg_match('/offline/i', $agent))
        {
            $os = 'offline';
        }
        else
        {
            $os = '未知操作系统';
        }
        return $os;
    }


}